package com.store.service.impl;

import com.store.entity.Address;
import com.store.entity.Cart;
import com.store.entity.Order;
import com.store.entity.OrderItem;
import com.store.mapper.CartMapper;
import com.store.mapper.OrderMapper;
import com.store.service.AddressService;
import com.store.service.OrderService;
import com.store.service.ex.*;
import com.store.vo.CartVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 订单业务层
 */
@DubboService(interfaceClass = OrderService.class,version = "1.0.0",timeout = 5000)
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Autowired(required = false)
    private CartMapper cartMapper;

    @DubboReference(interfaceClass = AddressService.class,version = "1.0.0",timeout = 5000)
    private AddressService addressService;


    /**
     * 新增订单
     * @param uid 用户id
     * @param aid 收获地址id
     * @param username 执行者
     * @param cids 购物车id数组
     */
    @Override
    public Integer addOrder(Integer uid, Integer aid, String username, Integer[] cids) {
        //查询购物车数据
        List<CartVO> voList = cartMapper.selsectVoByCid(cids);
        //总价计算
        Long total = 0L;
        for(CartVO v : voList){
            total += v.getTotalPrice()*v.getNum();
        }
        //获取收获地址
        Address address = addressService.getAddress(aid, uid);
        //封装订单数据
        Order order = new Order();
        order.setUid(uid);
        order.setRecvName(address.getName());
        order.setRecvPhone(address.getPhone());
        order.setRecvAddress(address.getAddress());
        order.setTotalPrice(total);
        order.setStatus(0);
        order.setOrderTime(new Date());
        order.setPayTime(null);
        order.setCreatedUser(username);
        order.setCreatedTime(new Date());
        order.setModifiedUser(username);
        order.setModifiedTime(new Date());
        //插入订单数据
        int rows = orderMapper.addOrder(order);
        if(rows != 1){
            throw new InsertException("插入订单数据时出现未知的异常");
        }
        //创建并插入订单项的数据
        for(CartVO v : voList){
            OrderItem orderItem = new OrderItem();
            orderItem.setOid(order.getOid());
            orderItem.setPid(v.getPid());
            orderItem.setTitle(v.getTitle());
            orderItem.setImage(v.getImage());
            orderItem.setPrice(v.getSinglePrice());
            orderItem.setNum(v.getNum());
            orderItem.setCreatedTime(new Date());
            orderItem.setCreatedUser(username);
            orderItem.setModifiedTime(new Date());
            orderItem.setModifiedUser(username);
            rows = orderMapper.addOrderItem(orderItem);
            if(rows != 1){
                throw new InsertException("插入订单项数据失败");
            }
        }
        return order.getOid();
    }

    /**
     * 更改订单信息
     * @param oid 订单id
     * @param cids 购物车id数组
     */
    @Override
    public void updateOrder(Integer oid,Integer[] cids) {
        Order exists = orderMapper.selectOrderByOid(oid);
        if(exists == null){
            throw new OrderNotFoundException("订单不存在！");
        }
        else {
            int rows = orderMapper.updateOrder(oid, 1, new Date());
            if(rows != 1){
                throw new UpdateException("更新订单信息时出现未知的异常");
            }
            for (int i = 0; i < cids.length; i++) {
                Cart cart = cartMapper.selectCartByCid(cids[i]);
                if (cart == null) {
                    throw new CartNotFoundException("购物车数据找不到异常");
                }
            }
            rows = cartMapper.deleteCart(cids);
            if(rows <= 0){
                throw new DeleteException("删除购物车数据产生未知的异常");
            }
        }
    }
}
